import json
import sys
import matplotlib.pyplot as plt

def generate_plot(data):
    dates = data['date']
    times_str = data['times']  # 假设times是字符串数组
    times = [int(t) for t in times_str]  # 将字符串数组转换为浮点数数组

    plt.figure(figsize=(10, 6))
    plt.plot(dates, times, marker='o')
    plt.title('xts failed times')
    plt.xlabel('Date')
    plt.ylabel('Time')
    plt.xticks(rotation=45)  # Rotate x-axis labels for better readability
    plt.grid(True)
    # 遍历数据点并添加文本标签
    for x, y in zip(dates, times):
        plt.text(x, y, f'{y:.2f}', ha='center', va='bottom')
    plt.tight_layout()
    plt.savefig('./linechart.png')  # 保存图表到文件
    plt.close()

if __name__ == '__main__':
    # 检查命令行参数数量
    if len(sys.argv) != 2:
        print("Usage: python generate_linechart.py <json_data>")
        sys.exit(1)
    # 获取JSON数据参数
    json_data = sys.argv[1]
    # 去除参数两边的引号
    json_data = json_data.strip("'")
    # 解析JSON数据
    data = json.loads(json_data)
    # 生成图表
    generate_plot(data)